package com.mlt.back.web.servlet;

import com.alibaba.fastjson.JSON;
import com.mlt.back.beans.Category;
import com.mlt.back.service.CategoryService;
import com.mlt.back.service.impl.CategoryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet({"/back/category"})
public class CategoryServlet extends HttpServlet {
    private CategoryService categoryService=new CategoryServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String servletPath = request.getServletPath();
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        if ("/back/category".equals(servletPath)){
            doSelect(request,response);
        }
    }

    /**
     * 陈雪融
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void doSelect(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<Category> categoryList=categoryService.selectAll();
        String s = JSON.toJSONString(categoryList);
        response.getWriter().print(s);
    }
}
